//
//  MineDynamicDetailViewController.m
//  WeCommunity
//
//  Created by mac on 2017/6/23.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MineDynamicDetailViewController.h"
#import "DynamicCell.h"
#import "FullScreenImage.h"
#import "CWebViewController.h"

@interface MineDynamicDetailViewController ()<UITableViewDelegate,UITableViewDataSource,DynamicClickContentDelegate>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSMutableArray * dataSource;

@end

@implementation MineDynamicDetailViewController

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreen_Width, KScreen_Height - KNaviheight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = WHITE_COLOR;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataSource = [@[self.detailModel] mutableCopy];
    
    [self initWithUserInterface];
    
    [self.tableView reloadData];
}

-(void)initWithUserInterface
{
    self.title = KNSLocal(@"mineSentContentTitle");
    
    [self.view addSubview:self.tableView];
    
}

#pragma mark - tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DynamicModel * model = self.dataSource[indexPath.row];
    return model.totalHeight;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"mineDynamicCell";
    DynamicCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell =[[DynamicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.delegate = self;
    }
    [cell creatCellWithModel:self.dataSource[indexPath.row] showDelete:YES showReportCount:NO];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - tableviewCell delegate
#pragma mark - 点赞.举报.more
-(void)likeDynamicWithSender:(UIButton *)sender clickCell:(DynamicCell *)cell
{
    NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
    DynamicModel * model = self.dataSource[cellIndexPath.row];
    if (sender.tag == LikeBtn_tag) { //点赞
        @weakify(self);
        [[DynamicServer shared] likeOrUnLikeDataWithDynamic:model complete:^(BOOL success, NSString *error, DynamicModel *model) {
            @strongify(self);
            if (success) {
                [cell changeLikeNumWithDynamic:model];
            }else{
                [UITool showHudWithText:error withView:self.view];
            }
        }];
    }else if (sender.tag == ReportBtn_tag){ //删除

        [self systemAlertWithTitle:nil andMessage:KNSLocal(@"deleteMineDynamic") actionTextOne:KNSLocal(@"alertCancel") actionTextTwo:KNSLocal(@"deleteTitle") actionOneBlock:^{
            
        } actionTwoBlock:^{
            [self deleteMineDynamic:model];
        }];
        
    }else if (sender.tag == ContactBtn_tag){ // 点击用户头像
        
        NSMutableArray * fullImageArray = [NSMutableArray array];
        FullScreenImage * imageModel = [FullScreenImage new];
        imageModel.fullImagePath = model.contact.headImage;
        imageModel.showImageView = (UIImageView *)sender;
        [fullImageArray addObject:imageModel];
        
        DealPhotosView * dealPhotosView = [[DealPhotosView alloc] init];
        [dealPhotosView setFullScreenWithImageArray:fullImageArray showIndex:0];
        [[[UIApplication sharedApplication].delegate window] addSubview:dealPhotosView];
    }
}

#pragma mark - 删除我发的动态
-(void)deleteMineDynamic:(DynamicModel *)model
{
    @weakify(self);
    [[DynamicServer shared] deleteDynamicWithObjectId:model.objectId complete:^(BOOL success, NSString *error) {
        @strongify(self);
        if (success) {
            [[NSNotificationCenter defaultCenter] postNotificationName:DELETE_CONTENT_NOTIKEY object:model.objectId];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [UITool showHudWithText:error withView:self.view];
        }
    }];
}

#pragma mark - 点击文字中的链接
-(void)processCommunityUrlLink:(NSString *)link cell:(DynamicCell *)cell
{
    CWebViewController * webVC = [[CWebViewController alloc] init];
    webVC.linkUrl = link;
    [self.navigationController pushViewController:webVC animated:YES];
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    UIMenuController * menu = [UIMenuController sharedMenuController];
    [menu setMenuVisible:NO animated:YES];
}

@end
